//
// Created by 08607 on 2022/12/24.
//

#include <tremo_lpuart.h>
#include <stdint.h>
#include "main.h"


#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define GETCHAR_PROTOTYPE int __io_getchar(void)
#else
#define PUTCHAR_PROTOTYPE int stdio_putchar(int char)
#define GETCHAR_PROTOTYPE int stdio_getchar(void)
#endif /* __GNUC__ */
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
int __io_putchar(int ch)
{
    usart_data_transmit(DEBUG_UART, ch);
    while(RESET == usart_flag_get(DEBUG_UART, USART_FLAG_TBE));
    return (ch);
}

int __io_getchar(void)
{
    int ch;
    if (RESET == usart_flag_get(DEBUG_UART, USART_FLAG_RBNE))
        ch = 0xff;
    else
        ch = usart_data_receive(DEBUG_UART);
    return (ch);
}

/**
  Put a character to the stdout

  \param[in]   ch  Character to output
  \return          The character written, or -1 on write error.
*/
int stdout_putchar (int ch) {
    usart_data_transmit(DEBUG_UART, ch);
    while(RESET == usart_flag_get(DEBUG_UART, USART_FLAG_TBE));
    return (ch);
}

/**
  Get a character from stdin

  \return     The next character from the input, or -1 on read error.
*/
int stdin_getchar (void) {
    int ch;
    if (RESET == usart_flag_get(DEBUG_UART, USART_FLAG_RBNE))
        ch = 0xff;
    else
        ch = usart_data_receive(DEBUG_UART);
    return (ch);
}
